package com.fdd.controller;

import com.fdd.domain.Book;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/books")
public class BookController {
    @GetMapping("/{id}")
    String getById(@PathVariable Integer id) {
        System.out.println("getById:" + id);
        return "{'code':0, 'msg':'getById'}";
    }

    @PostMapping
    public String save(@RequestBody Book book) {
        System.out.println("book save ==> " + book);
        return "{'code':0, 'msg':'save'}";
    }

    @GetMapping
    List<Book> getAll() {
        List<Book> bookList = new ArrayList<Book>();

        Book book1 = new Book();
        book1.setId(1);
        book1.setType("计算机");
        book1.setName("SpringMVC入门级从");
        book1.setDescription("欣赏你的");

        Book book2 = new Book();
        book2.setId(2);
        book2.setType("种子");
        book2.setName("一桌子饭菜");
        book2.setDescription("母主的产后护理");

        bookList.add(book1);
        bookList.add(book2);

        System.out.println("getAll ...");
        return bookList;
    }

    @PostMapping("/{id}")
    String setById(@PathVariable Integer id) {
        System.out.println("setById ..." + id);
        return "{'code':0, 'msg':'setById'}";
    }

    @DeleteMapping("/{id}")
    String delete(@PathVariable Integer id) {
        System.out.println("delete ..." + id);
        return "{'code':0, 'msg':'delete'}";
    }
}
